﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Reflection
Imports System.Runtime.Serialization
Imports System.Security.Permissions

Namespace System.Workflow.ComponentModel.Design
    <Serializable, PermissionSet(SecurityAction.InheritanceDemand, Name:="FullTrust"), PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")> _
    Public Class ActivityToolboxItem
        Inherits ToolboxItem
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal type As Type)
            MyBase.New(type)
            If (Not type Is Nothing) Then
                If (Not type.Name Is Nothing) Then
                    Dim name As String = type.Name
                    If (((type.Assembly Is Assembly.GetExecutingAssembly) OrElse (((Not type.Assembly Is Nothing) AndAlso (Not type.Assembly.FullName Is Nothing)) AndAlso type.Assembly.FullName.Equals("System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", StringComparison.OrdinalIgnoreCase))) AndAlso (type.Name.EndsWith("Activity", StringComparison.Ordinal) AndAlso Not type.Name.Equals("Activity", StringComparison.Ordinal))) Then
                        name = type.Name.Substring(0, (type.Name.Length - "Activity".Length))
                    End If
                    MyBase.DisplayName = name
                End If
                MyBase.Description = ActivityDesigner.GetActivityDescription(type)
            End If
        End Sub

        Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
            Me.Deserialize(info, context)
        End Sub

        Protected Overrides Function CreateComponentsCore(ByVal host As IDesignerHost) As IComponent()
            Dim c As Type = Me.GetType(host, MyBase.AssemblyName, MyBase.TypeName, True)
            If ((c Is Nothing) AndAlso (Not host Is Nothing)) Then
                c = host.GetType(MyBase.TypeName)
            End If
            If (c Is Nothing) Then
                Dim service As ITypeProviderCreator = Nothing
                If (Not host Is Nothing) Then
                    service = DirectCast(host.GetService(GetType(ITypeProviderCreator)), ITypeProviderCreator)
                End If
                If (Not service Is Nothing) Then
                    Dim transientAssembly As Assembly = service.GetTransientAssembly(MyBase.AssemblyName)
                    If (Not transientAssembly Is Nothing) Then
                        c = transientAssembly.GetType(MyBase.TypeName)
                    End If
                End If
                If (c Is Nothing) Then
                    c = Me.GetType(host, MyBase.AssemblyName, MyBase.TypeName, True)
                End If
            End If
            Dim list As New ArrayList
            If ((Not c Is Nothing) AndAlso GetType(IComponent).IsAssignableFrom(c)) Then
                list.Add(TypeDescriptor.CreateInstance(Nothing, c, Nothing, Nothing))
            End If
            Dim array As IComponent() = New IComponent(list.Count  - 1) {}
            list.CopyTo(array, 0)
            Return array
        End Function

        Public Overridable Function CreateComponentsWithUI(ByVal host As IDesignerHost) As IComponent()
            Return Me.CreateComponentsCore(host)
        End Function

        Public Shared Function GetToolboxDisplayName(ByVal activityType As Type) As String
            If (activityType Is Nothing) Then
                Throw New ArgumentNullException("activityType")
            End If
            Dim name As String = activityType.Name
            Dim customAttributes As Object() = activityType.GetCustomAttributes(GetType(ToolboxItemAttribute), True)
            If ((Not customAttributes Is Nothing) AndAlso (customAttributes.Length > 0)) Then
                Dim attribute As ToolboxItemAttribute = [TryCast](customAttributes(0),ToolboxItemAttribute)
                If ((Not attribute Is Nothing) AndAlso (Not attribute.ToolboxItemType Is Nothing)) Then
                    Try 
                        Dim item As ToolboxItem = [TryCast](Activator.CreateInstance(attribute.ToolboxItemType, New Object() { activityType }),ToolboxItem)
                        If (Not item Is Nothing) Then
                            name = item.DisplayName
                        End If
                    Catch obj1 As Object
                    End Try
                End If
            End If
            If ((((Not activityType.Assembly Is Nothing) AndAlso (Not activityType.Assembly.FullName Is Nothing)) AndAlso (activityType.Assembly.FullName.Equals("System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", StringComparison.OrdinalIgnoreCase) OrElse activityType.Assembly.FullName.Equals(Assembly.GetExecutingAssembly.FullName, StringComparison.OrdinalIgnoreCase))) AndAlso (name.EndsWith("Activity", StringComparison.Ordinal) AndAlso Not name.Equals("Activity", StringComparison.Ordinal))) Then
                name = name.Substring(0, (name.Length - "Activity".Length))
            End If
            Return name
        End Function

        Public Shared Function GetToolboxImage(ByVal activityType As Type) As Image
            If (activityType Is Nothing) Then
                Throw New ArgumentNullException("activityType")
            End If
            Dim image As Image = Nothing
            If (Not activityType Is Nothing) Then
                Dim customAttributes As Object() = activityType.GetCustomAttributes(GetType(ToolboxBitmapAttribute), False)
                If ((Not customAttributes Is Nothing) AndAlso (customAttributes.GetLength(0) = 0)) Then
                    customAttributes = activityType.GetCustomAttributes(GetType(ToolboxBitmapAttribute), True)
                End If
                Dim attribute As ToolboxBitmapAttribute = If(((Not customAttributes Is Nothing) AndAlso (customAttributes.GetLength(0) > 0)), [TryCast](customAttributes(0),ToolboxBitmapAttribute), Nothing)
                If (Not attribute Is Nothing) Then
                    image = attribute.GetImage(activityType)
                End If
            End If
            Return image
        End Function


        ' Fields
        Private Const ActivitySuffix As String = "Activity"
    End Class
End Namespace

